<script setup>
import {ref} from 'vue'
import useUserInfoStore from "@/stores/userInfo";
import {useRouter} from "vue-router";
import {BookDetailService} from "@/apis/book";
import {ElMessage} from "element-plus";
import {useCartStore} from "@/stores/shopcarStore";

const cartStore = useCartStore()
const userInfoStore = useUserInfoStore()
const router = useRouter()

const itemChecked = (item, selected) => {
    cartStore.singleCheck(item.id, selected)
}

const allCheck = (selected) => {
    cartStore.allCheck(selected)
}

const orderBooks = () => {
    if (cartStore.selectedCount !== 0) {
        router.push('/order')
    } else {
        ElMessage.warning('请选择至少1件商品')
    }
}

const goodsMax = ref([])
const getGoods = async (list) => {
    for (let i = 0; i < list.length; i++) {
        const res = await BookDetailService(list[i].id)
        goodsMax.value[i] = res.data.stock
    }
}
cartStore.updateCart()
</script>

<template>
    <div class="c-container clearfix">
        <div class="wrapper">
            <div class="shopcar-filter-bar">
                <span>我的购物车</span>
            </div>
            <div class="c-main">
                <div class="c-head">
                    <div class="t-checkbox">
                        <el-checkbox :model-value="cartStore.checkAll" @change="allCheck"/>
                        全选
                    </div>
                    <div class="t-goods">商品</div>
                    <div class="t-price">单价</div>
                    <div class="t-num">数量</div>
                    <div class="t-sum">小计</div>
                    <div class="t-action">操作</div>
                </div>

                <div class="c-item-list" >
                    <div v-if="cartStore.cartList == 0">
                        <h2>还没有商品哦</h2>
                    </div>
                    <div v-else>
                        <ul>
                            <li v-for="(item, index) in cartStore.cartList" :key="item.id">
                                <div class="c-item">
                                    <div class="p-checkbox">
                                        <el-checkbox :model-value="item.selected"
                                                     @change="(selected) => itemChecked(item, selected)"/>
                                    </div>
                                    <div class="p-goods">
                                        <div class="p-img">
                                            <img :src="item.img" alt=""/>
                                        </div>
                                        <div class="p-msg">
                                            <a :href="`/detail/${item.bid}`">{{ item.name }}</a>
                                        </div>
                                    </div>
                                    <div class="p-price">￥{{ parseFloat(item.price).toFixed(2) }}</div>
                                    <div class="p-num">
                                        <el-input-number size="small" v-model="item.count" :min="1" :max="goodsMax[index]"
                                                         @change="cartStore.countChange(item.id)"
                                                         @blur="cartStore.countChange(item.id)"/>
                                    </div>
                                    <div class="p-sum">￥{{ (item.price * item.count).toFixed(2) }}</div>
                                    <div class="p-del"><a href="javascript:;" @click="cartStore.delCart(item.id)">删除</a>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <button type="submit" class="orderBuy" @click="orderBooks">结算</button>
                    <span
                        class="orderCount">共{{ cartStore.numCount }}件商品，已选择{{ cartStore.selectedCount }}件</span>
                    <span class="orderTotal">合计：<span>{{ cartStore.selectedPrice.toFixed(2) }}</span> 元</span>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.c-container .shopcar-filter-bar {
    border-bottom: 2px solid #b1191a;
    margin-top: 20px;
}

.c-container .shopcar-filter-bar span {
    font-size: 20px;
    color: #b1191a;
    font-weight: bold;
}

.c-container .c-head {
    position: relative;
    height: 32px;
    line-height: 32px;
    margin: 5px 0 10px;
    padding: 5px 0;
    background: #f3f3f3;
    border: 1px solid #e9e9e9;
    border-top: 0;
}

.c-head > div,
.c-item > div {
    float: left;
}

.t-checkbox,
.p-checkbox {
    height: 18px;
    line-height: 18px;
    padding-top: 7px;
    width: 122px;
    padding-left: 11px;
}


.t-goods {
    width: 400px;
}

.t-price {
    width: 120px;
    padding-right: 40px;
    text-align: right;
}

.t-num {
    width: 200px;
    text-align: center;
}

.t-sum {
    width: 95px;
    text-align: right;
}

.t-action {
    width: 130px;
    text-align: right;
}

.c-item-list li {
    height: 150px;
    border: 1px solid #e8e8e8;
    border-top: 2px solid #a3a3a3;
}

.p-checkbox {
    float: left;
    width: 50px;
    margin-top: 20px;
}

.p-img {
    float: left;
    margin-top: 20px;
    padding: 5px;
    border: 1px solid #a3a3a3;

}

.p-img img {
    width: 80px;
    height: 80px;
    object-fit: contain;
}

.p-msg {
    float: left;
    margin-left: 20px;
    margin-top: 20px;
    width: 250px;
    overflow: hidden;
}

.p-price {
    float: left;
    margin-left: 200px;
    margin-top: 20px;
    width: 50px;
}

.p-num {
    float: left;
    margin-left: 65px;
    margin-top: 20px;
}

.p-sum {
    float: left;
    margin-top: 20px;
    margin-left: 100px;
    width: 50px;
}

.p-del {
    float: left;
    margin-top: 20px;
    margin-left: 80px;
    padding: 5px 10px;
    background-color: #b1191a;
}

.p-del a {
    color: #fff;
}

.c-item-list .orderTotal {
    float: right;
    margin-top: 20px;
    margin-right: 50px;
    font-size: 18px;
}

.c-item-list .orderBuy {
    float: right;
    margin-top: 20px;
    margin-right: 50px;
    padding: 5px 50px;
    font-size: 24px;
    border: 0;
    background-color: #b1191a;
    color: #fff;
    border-radius: 20px;
}

.c-item-list .orderTotal span {
    color: #b1191a;
}

.orderCount {
    float: left;
    margin-top: 20px;
    font-size: 18px;
}

</style>